<?php
/**
 * @author Adam Zivner <adam.zivner@gmail.com>
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD license
 * @package AttoMVC https://code.google.com/p/attomvc/
 */

class AttoView {
	private $dir = null;

	/** @var string - default render script */
	public $script_path = null;

	public function __construct()
	{
		$this->dir = APPLICATION_PATH . "/view";
	}

	public function getViewDirectory()     { return $this->dir; }
	public function setViewDirectory($dir) { $this->dir = rtrim($dir, "/"); }

	/**
	 * Render view script and return the output.
	 *
	 * @param string $script_path - path to the view script
	 * @return string - view output
	 */

	public function render($script_path = null)
	{
		if(!$script_path && $this->script_path)
			$script_path = $this->script_path;

		if($script_path) {
			$path = $this->dir . "/" . $script_path;
			
			if(!file_exists($path))
				throw new Exception("View file not found!");
			else {
				ob_start(); // catch the output

				include($path);

				$ret = ob_get_contents();

				ob_end_clean();

				return $ret;
			}
		}
	}

	// Following are helper functions used mainly by view scripts

	public function escape($str)
	{
		return htmlspecialchars($str);
	}

	public function url($params)
	{
		return AttoHelperURL::getInstance()->url($params);
	}

}